package com.zt.bus.order.controller;

import com.zt.bus.line.service.LinePlanService;
import com.zt.bus.order.model.Passenger;
import com.zt.bus.order.service.OrderAmountService;
import com.zt.bus.order.service.OrderService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;
import com.zt.system.model.BusinessException;
import com.zt.util.DateUtil;
import com.zt.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    private LinePlanService linePlanService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderAmountService orderAmountService;

    /**
     * 查询订单，后台管理
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(orderService.getTable(reqBean));
    }

    /**
     * 步骤一：获取订单折扣及折扣后价格
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/getDiscount")
    @ResponseBody
    public JSONObject getDiscount(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        String passengerCount = reqBean.getInfo("passengerCount");
        return Response.response(orderAmountService.getDiscount(reqBean.getInfo("lineId"), reqBean.getInfo("planId"),
                getDates(reqBean), StringUtil.parseInt(passengerCount, 1)));
    }

    /**
     * 步骤二：创建订单
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/create")
    @ResponseBody
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        List<Passenger> passengers = new ArrayList<>();
        String passengerStr = reqBean.getInfo("passengers");
        if (StringUtil.isEmpty(passengerStr)) {
            throw new BusinessException("请选择乘客信息");
        }
        JSONArray array = JSONArray.fromObject(passengerStr);
        for (int i = 0; i < array.size(); i++) {
            JSONObject j = array.getJSONObject(i);
            passengers.add(new Passenger(j.getString("name"), j.getString("phone"), j.getString("onStopId"), j
                    .optString("offStopId")));
        }
        String orderId = orderService.create(reqBean.getInfo("lineId"), reqBean.getInfo("planId"),
                reqBean.getInfo("discountId"), passengers, getDates(reqBean));
        return Response.response(orderId);
    }

    /**
     * 待支付订单
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/toBePaid")
    @ResponseBody
    public JSONObject getToBePaid(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(orderService.getToBePaid(reqBean.getUserId()));
    }

    /**
     * 已支付订单
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/hadPaid")
    @ResponseBody
    public JSONObject getHasPaid(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(orderService.getHasPaid(reqBean.getUserId()));
    }

    /**
     * 订单明细
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/details")
    @ResponseBody
    public JSONObject getDetails(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(orderService.getDetails(reqBean.getInfo("orderId")));
    }

    /**
     * 可退订订单明细
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/canBackDetails")
    @ResponseBody
    public JSONObject getCanBackDetails(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(orderService.getCanBackDetails(reqBean.getInfo("orderId")));
    }

    /**
     * 用户取消订单
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/cancel")
    @ResponseBody
    public JSONObject cancelOrder(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        orderService.cancel(reqBean.getInfo("orderId"));
        return Response.responseSuccess();
    }

    /**
     * 获取乘车验证码
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/busCode")
    @ResponseBody
    public JSONObject busCode(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return orderService.busCode(reqBean.getInfo("orderId"), reqBean.getInfo("passengerName"),
                reqBean.getInfo("passengerPhone"));
    }

    /**
     * 获取统计日期
     * 
     * @param reqBean
     * @return
     */
    private List<Date> getDates(ReqBean reqBean) {
        List<Date> list = new ArrayList<>();
        String dates = reqBean.getInfo("dates");
        if (StringUtil.isEmpty(dates)) {
            throw new BusinessException("请选择乘车日期");
        }
        String[] dateArr = dates.split(",");
        for (int i = 0; i < dateArr.length; i++) {
            list.add(DateUtil.parse(dateArr[i]));
        }
        return list;
    }
}
